#include "../facecat/stdafx.h"
#include "QtPaint.h"

MyColor::MyColor(){
    m_lastStyle = -1;
}
MyColor::~MyColor(){
    
}

static String m_systemFont;

void MyColor::setSystemFont(String value){
    m_systemFont = value;
}

String MyColor::getSystemFont(){
    if ((int)m_systemFont.length() == 0)
    {
        m_systemFont = L"Segoe UI";
    }
    return m_systemFont;
}

static int m_style = 0;

int MyColor::getStyle(){
    return m_style;
}
void MyColor::setStyle(int value){
    m_style = value;
}

void MyColor::createColors(){ 
	m_userColors.clear();
	if (m_style == 1)
    {
        m_userColors.put(FCColor_Back, FCColor::rgb(255, 255, 255)); //1
        m_userColors.put(FCColor_Border, FCColor::rgb(200, 200, 200)); //1
        m_userColors.put(FCColor_Text, FCColor::rgb(0, 0, 0)); //1
        m_userColors.put(FCColor_DisabledBack, FCColor::rgba(0, 0, 0, 25)); //1
        m_userColors.put(FCColor_DisabledText, 3289650);
        m_userColors.put(FCColor_Hovered, FCColor::rgba(43, 138, 195, 150));
        m_userColors.put(FCColor_Pushed, FCColor::rgba(43, 138, 195, 100));
    }else{
        m_userColors.put(FCColor_Back, FCColor::rgb(19, 21, 26));
        m_userColors.put(FCColor_Border, FCColor::rgb(50, 50, 50));
        m_userColors.put(FCColor_Text, FCColor::rgb(255, 255, 255));
        m_userColors.put(FCColor_DisabledBack, FCColor::rgba(255, 255, 255, 25));
        m_userColors.put(FCColor_DisabledText, 3289650);
        m_userColors.put(FCColor_Hovered, FCColor::rgba(43, 138, 195, 150));
        m_userColors.put(FCColor_Pushed, FCColor::rgba(43, 138, 195, 100));
    }
}

Long MyColor::getUserColor(Long color){
    if (m_lastStyle != m_style)
    {
        m_lastStyle = m_style;
       createColors();
    }
    if (m_userColors.containsKey(color))
    {
       color = m_userColors.get(color);
    }
    return color;
}

void QtPaint::affectScaleFactor(QRect *qRect){
    if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
        QRect newRect(qRect->x() * m_scaleFactorX, qRect->y() * m_scaleFactorY,
                      qRect->width() * m_scaleFactorX, qRect->height() * m_scaleFactorY);
        *qRect = newRect;
    }
}

void QtPaint::affectScaleFactor(QRectF *qRect){
    if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
        QRectF newRect(qRect->x() * m_scaleFactorX, qRect->y() * m_scaleFactorY,
                      qRect->width() * m_scaleFactorX, qRect->height() * m_scaleFactorY);
        *qRect = newRect;
    }
}

QColor QtPaint::getQColor(Long dwPenColor){
    dwPenColor = getPaintColor(dwPenColor);
    int a = 255;
    if(dwPenColor < 0){
        dwPenColor = -dwPenColor;
        if(dwPenColor < 1){
            a = 255;
        }
        else{
            a = (int)(dwPenColor - dwPenColor / 1000 * 1000);
        }
        dwPenColor /= 1000;
    }
    int r = (int)(dwPenColor & 0xff);
    int g = (int)((dwPenColor >> 8) & 0xff);
    int b = (int)((dwPenColor >> 0x10) & 0xff);
    QColor gdiColor(r, g, b, a);
    if(m_opacity < 1){
        QColor opacityColor(r, g, b, a * m_opacity);
        return opacityColor;
    }
    return gdiColor;
}

QFont QtPaint::getFont(FCFont *font){
	String fontFamily = font->m_fontFamily;
	if(fontFamily == L"Default"){
		fontFamily = MyColor::getSystemFont();
	}
    QFont qFont(toQString(font->m_fontFamily), font->m_fontSize / m_fontScale);
    qFont.setBold(font->m_bold);
    qFont.setItalic(font->m_italic);
    qFont.setStrikeOut(font->m_strikeout);
    return qFont;
}

QString QtPaint::toQString(const String& str){
    lstring lstr = FCTran::StringTostring(str);
    return QString(lstr.c_str());
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

QtPaint::QtPaint(){
    m_exportPath = L"";
    m_fontScale = 2.1;
    m_isPathStart = false;
	m_offsetX = 0;
	m_offsetY = 0;
    m_opacity = 1;
	m_pRect.left = 0;
	m_pRect.top = 0;
	m_pRect.right = 0;
	m_pRect.bottom = 0;
    m_path = 0;
    m_qPainter = 0;
	m_resourcePath = L"";
	m_rotateAngle = 0;
	m_scaleFactorX = 0;
    m_scaleFactorY = 0;
	m_wRect.left = 0;
	m_wRect.top = 0;
	m_wRect.right = 0;
	m_wRect.bottom = 0;
    m_myColor = new MyColor;
}

QtPaint::~QtPaint(){
    if(m_path){
        delete m_path;
    }
    m_qPainter = 0;
    m_path = 0;
	clearCaches();
	if(m_myColor){
		delete m_myColor;
		m_myColor = 0;
	}
}

void QtPaint::addArc(const FCRect& rect, float startAngle, float sweepAngle){
    int rw = rect.right - rect.left - 1;
    if (rw < 1) rw = 1;
    int rh = rect.bottom - rect.top - 1;
    if (rh < 1) rh = 1;
    QRect qRect(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
    affectScaleFactor(&qRect);
}

void QtPaint::addBezier(FCPoint *apt, int cpt){
    QPoint *points = new QPoint[cpt];
	for(int i = 0; i < cpt; i++){
		int x = apt[i].x + m_offsetX;
		int y = apt[i].y + m_offsetY;
		if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
			x = (int)(m_scaleFactorX * x);
			y = (int)(m_scaleFactorY * y);
		}
        QPoint newPoint(x, y);
		points[i] = newPoint;
    }
	delete[] points;
	points = 0;
}

void QtPaint::addCurve(FCPoint *apt, int cpt){
    QPoint *points = new QPoint[cpt];
	for(int i = 0; i < cpt; i++){
		int x = apt[i].x + m_offsetX;
		int y = apt[i].y + m_offsetY;
		if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
			x = (int)(m_scaleFactorX * x);
			y = (int)(m_scaleFactorY * y);
		}
        QPoint newPoint(x, y);
		points[i] = newPoint;
    }
	delete[] points;
	points = 0;
}

void QtPaint::addEllipse(const FCRect& rect){
    int rw = rect.right - rect.left - 1;
    if (rw < 1) rw = 1;
    int rh = rect.bottom - rect.top - 1;
    if (rh < 1) rh = 1;
    QRect qRect(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
    affectScaleFactor(&qRect);
    m_path->addEllipse(qRect);
}

void QtPaint::addLine(int x1, int y1, int x2, int y2){
    int lx1 = x1 + m_offsetX;
    int ly1 = y1 + m_offsetY;
    int lx2 = x2 + m_offsetX;
    int ly2 = y2 + m_offsetY;
    if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
        lx1 = (int)(m_scaleFactorX * lx1);
        ly1 = (int)(m_scaleFactorY * ly1);
        lx2 = (int)(m_scaleFactorX * lx2);
        ly2 = (int)(m_scaleFactorY * ly2);
    }
    if(m_isPathStart){
        m_isPathStart = false;
        m_path->moveTo(lx1, ly1);
    }
    m_path->lineTo(lx2, ly2);
}

void QtPaint::addRect(const FCRect& rect){
    int rw = rect.right - rect.left - 1;
    if (rw < 1) rw = 1;
    int rh = rect.bottom - rect.top - 1;
    if (rh < 1) rh = 1;
    QRect qRect(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
    affectScaleFactor(&qRect);
    m_path->addRect(qRect);
}

void QtPaint::addPie(const FCRect& rect, float startAngle, float sweepAngle){
    int rw = rect.right - rect.left - 1;
    if (rw < 1) rw = 1;
    int rh = rect.bottom - rect.top - 1;
    if (rh < 1) rh = 1;
    QRect qRect(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
    affectScaleFactor(&qRect);
}

void QtPaint::addText(String strText, FCFont *font, const FCRect& rect, int width){
}

void QtPaint::beginExport(const String& exportPath, const FCRect& rect){
	m_exportPath = exportPath;
    int imageW = rect.right - rect.left;
    int imageH = rect.bottom - rect.top;
    if (imageW == 0) imageW = 1;
    if (imageH == 0) imageH = 1;
    m_opacity = 1;
    m_resourcePath = L"";
}

void QtPaint::beginPaint(int hDC, const FCRect& wRect, const FCRect& pRect){
	m_pRect = pRect;
	m_wRect = wRect;
	int width = m_wRect.right - m_wRect.left;
    int height = m_wRect.bottom - m_wRect.top;
	m_opacity = 1;
    m_resourcePath = L"";
}

void QtPaint::beginPath(){
    m_path = new QPainterPath();
    m_isPathStart = true;
}

void QtPaint::clearCaches(){
}

void QtPaint::clipPath(){
}

void QtPaint::closeFigure(){
    m_path->closeSubpath();
}

void QtPaint::closePath(){
    delete m_path;
    m_path = 0;
    m_isPathStart = false;
}

void QtPaint::drawArc(Long dwPenColor, float width, int style, const FCRect& rect, float startAngle, float sweepAngle){
}

void QtPaint::drawBezier(Long dwPenColor, float width, int style, FCPoint *apt, int cpt){
}

void QtPaint::drawCurve(Long dwPenColor, float width, int style, FCPoint *apt, int cpt){
}

void QtPaint::drawEllipse(Long dwPenColor, float width, int style, const FCRect& rect){
	if(dwPenColor == FCColor_None) return;
	int rw = rect.right - rect.left - 1;
	if(rw < 1) rw = 1;
	int rh = rect.bottom - rect.top - 1;
	if(rh < 1) rh = 1;
    QRect qRect(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
    affectScaleFactor(&qRect);
    QPen pen(getQColor(dwPenColor));
    float lineWidth = std::min(m_scaleFactorX, m_scaleFactorY) * width;
    pen.setWidth(lineWidth);
    if(style == 1){
        pen.setStyle(Qt::DotLine);
    }else if(style == 2){
        pen.setStyle(Qt::DashLine);
    }
    m_qPainter->setPen(pen);
    m_qPainter->setBrush(QColor(0, 0, 0, 0));
    m_qPainter->drawEllipse(qRect);
}

void QtPaint::drawEllipse(Long dwPenColor, float width, int style, int left, int top, int right, int bottom){
    if(dwPenColor == FCColor_None) return;
    FCRect rect = {left, top, right, bottom};
    drawEllipse(dwPenColor, width, style, rect);   
}

void QtPaint::drawImage(String imagePath, const FCRect& rect){
}

void QtPaint::drawLine(Long dwPenColor, float width, int style, const FCPoint& x, const FCPoint& y){
	drawLine(dwPenColor, width, style, x.x, x.y, y.x, y.y);
}

void QtPaint::drawLine(Long dwPenColor, float width, int style, int x1, int y1, int x2, int y2){
	if(dwPenColor == FCColor_None) return;
	int lx1 = x1 + m_offsetX;
    int ly1 = y1 + m_offsetY;
    int lx2 = x2 + m_offsetX;
    int ly2 = y2 + m_offsetY;
    if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
        lx1 = (int)(m_scaleFactorX * (x1 + m_offsetX));
        ly1 = (int)(m_scaleFactorY * (y1 + m_offsetY));
        lx2 = (int)(m_scaleFactorX * (x2 + m_offsetX));
        ly2 = (int)(m_scaleFactorY * (y2 + m_offsetY));
    }
    QPen pen(getQColor(dwPenColor));
    float lineWidth = std::min(m_scaleFactorX, m_scaleFactorY) * width;
    pen.setWidth(lineWidth);
    if(style == 1){
        pen.setStyle(Qt::DotLine);
    }else if(style == 2){
        pen.setStyle(Qt::DashLine);
    }
    m_qPainter->setPen(pen);
    m_qPainter->setBrush(QColor(0, 0, 0, 0));
    m_qPainter->drawLine(lx1, ly1, lx2, ly2);
}

void QtPaint::drawPath(Long dwPenColor, float width, int style){
    if(dwPenColor == FCColor_None) return;
    QPen pen(getQColor(dwPenColor));
    float lineWidth = std::min(m_scaleFactorX, m_scaleFactorY) * width;
    pen.setWidth(lineWidth);
    if(style == 1){
        pen.setStyle(Qt::DotLine);
    }else if(style == 2){
        pen.setStyle(Qt::DashLine);
    }
    m_qPainter->setPen(pen);
    m_qPainter->setBrush(QColor(0, 0, 0, 0));
    m_qPainter->drawPath(*m_path);
}

void QtPaint::drawPie(Long dwPenColor, float width, int style, const FCRect& rect, float startAngle, float sweepAngle){
	int rw = rect.right - rect.left - 1;
    if (rw < 1) rw = 1;
    int rh = rect.bottom - rect.top - 1;
    if (rh < 1) rh = 1;
    QRect qRect(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
    affectScaleFactor(&qRect);
}

void QtPaint::drawPolygon(Long dwPenColor, float width, int style, FCPoint *apt, int cpt){
	if(dwPenColor == FCColor_None) return;
    QPoint *points = new QPoint[cpt];
	for(int i = 0; i < cpt; i++){
		int x = apt[i].x + m_offsetX;
		int y = apt[i].y + m_offsetY;
		if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
			x = (int)(m_scaleFactorX * x);
			y = (int)(m_scaleFactorY * y);
		}
        QPoint newPoint(x, y);
		points[i] = newPoint;
    }
    QPen pen(getQColor(dwPenColor));
    float lineWidth = std::min(m_scaleFactorX, m_scaleFactorY) * width;
    pen.setWidth(lineWidth);
    if(style == 1){
        pen.setStyle(Qt::DotLine);
    }else if(style == 2){
        pen.setStyle(Qt::DashLine);
    }
    m_qPainter->setPen(pen);
    m_qPainter->setBrush(QColor(0, 0, 0, 0));
    m_qPainter->drawPolygon(points, cpt);
	delete[] points;
	points = 0;
}

void QtPaint::drawPolyline(Long dwPenColor, float width, int style, FCPoint *apt, int cpt){
if(cpt == 1){
		drawLine(dwPenColor, width, style, apt[0].x, apt[0].y, apt[0].x, apt[0].y);
		return;
	}
	if(dwPenColor == FCColor_None) return;
    QPoint *points = new QPoint[cpt];
	int lx = 0, ly = 0;
	for(int i = 0; i < cpt; i++){
		int x = apt[i].x + m_offsetX;
		int y = apt[i].y + m_offsetY;
		if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
			x = (int)(m_scaleFactorX * x);
			y = (int)(m_scaleFactorY * y);
		}
        QPoint newPoint(x, y);
		points[i] = newPoint;
    }
    QPen pen(getQColor(dwPenColor));
    float lineWidth = std::min(m_scaleFactorX, m_scaleFactorY) * width;
    pen.setWidth(lineWidth);
    if(style == 1){
        pen.setStyle(Qt::DotLine);
    }else if(style == 2){
        pen.setStyle(Qt::DashLine);
    }
    m_qPainter->setPen(pen);
    m_qPainter->setBrush(QColor(0, 0, 0, 0));
    m_qPainter->drawPolyline(points, cpt);
	delete[] points;
	points = 0;
}

void QtPaint::drawRect(Long dwPenColor, float width, int style, int left, int top, int right, int bottom){
	FCRect rect = {left, top, right, bottom};
    drawRect(dwPenColor, width, style, rect);
}

void QtPaint::drawRect(Long dwPenColor, float width, int style, const FCRect& rect){
	if(dwPenColor == FCColor_None) return;
	int rw = rect.right - rect.left - 1;
	if(rw < 1) rw = 1;
	int rh = rect.bottom - rect.top - 1;
	if(rh < 1) rh = 1;
    QRect qRect(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
    affectScaleFactor(&qRect);
    QPen pen(getQColor(dwPenColor));
    float lineWidth = std::min(m_scaleFactorX, m_scaleFactorY) * width;
    pen.setWidth(lineWidth);
    if(style == 1){
        pen.setStyle(Qt::DotLine);
    }else if(style == 2){
        pen.setStyle(Qt::DashLine);
    }
    m_qPainter->setPen(pen);
    m_qPainter->setBrush(QColor(0, 0, 0, 0));
    m_qPainter->drawRect(qRect);
}

void QtPaint::drawRoundRect(Long dwPenColor, float width, int style, const FCRect& rect, int cornerRadius){
    if(cornerRadius > 0){
        if(dwPenColor == FCColor_None) return;
        int rw = rect.right - rect.left - 1;
        if(rw < 1) rw = 1;
        int rh = rect.bottom - rect.top - 1;
        if(rh < 1) rh = 1;
        QRect qRect(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
        affectScaleFactor(&qRect);
        QPen pen(getQColor(dwPenColor));
        float lineWidth = std::min(m_scaleFactorX, m_scaleFactorY) * width;
        pen.setWidth(lineWidth);
        if(style == 1){
            pen.setStyle(Qt::DotLine);
        }else if(style == 2){
            pen.setStyle(Qt::DashLine);
        }
        m_qPainter->setPen(pen);
        m_qPainter->setBrush(QColor(0, 0, 0, 0));
        int corner = (int)(min(m_scaleFactorX, m_scaleFactorY) * cornerRadius);
        m_qPainter->drawRoundedRect(qRect, corner, corner);
    }else{
        drawRect(dwPenColor, width, style, rect.left, rect.top, rect.right, rect.bottom);
    }
}

void QtPaint::drawText(String strText, Long dwPenColor, FCFont *font, const FCRect& rect, int width){
    if(dwPenColor == FCColor_None) return;
    m_qPainter->setPen(getQColor(dwPenColor));
    m_qPainter->setBrush(QColor(0, 0, 0, 0));
    if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
        float fontSize = (float)(font->m_fontSize * (m_scaleFactorX + m_scaleFactorY) / 2);
        FCFont scaleFont(font->m_fontFamily, fontSize, font->m_bold, font->m_underline, font->m_italic);
        QFont qFont = getFont(&scaleFont);
        m_qPainter->setFont(qFont);
    }else{
        QFont qFont = getFont(font);
        m_qPainter->setFont(qFont);
    }
    m_qPainter->drawText(int((rect.left + m_offsetX) * m_scaleFactorX), int((rect.bottom + m_offsetY) * m_scaleFactorY), toQString(strText));
}

void QtPaint::drawText(String strText, Long dwPenColor, FCFont *font, const FCRectF& rect, int width){
    if(dwPenColor == FCColor_None) return;
    FCRect dRect = {(int)rect.left, (int)rect.top, (int)rect.right, (int)rect.bottom};
    drawText(strText, dwPenColor, font, dRect, -1);
}

void QtPaint::drawTextAutoEllipsis(String strText, Long dwPenColor, FCFont *font, const FCRect& rect){
	if(dwPenColor == FCColor_None) return; 
    drawText(strText, dwPenColor, font, rect, -1);
}

void QtPaint::endExport(){
    m_offsetX = 0;
    m_offsetY = 0;
    m_opacity = 1;
    m_resourcePath = L"";
}

void QtPaint::endPaint(){
	m_offsetX = 0;
	m_offsetY = 0;
	m_opacity = 1;
	m_resourcePath = L"";
}

void QtPaint::excludeClipPath(){
}

void QtPaint::fillEllipse(Long dwPenColor, const FCRect& rect){
	if(dwPenColor == FCColor_None) return;
    QRect qRect(rect.left + m_offsetX, rect.top + m_offsetY, rect.right - rect.left, rect.bottom - rect.top);
    affectScaleFactor(&qRect);
    m_qPainter->setPen(QColor(0, 0, 0, 0));
    m_qPainter->setBrush(getQColor(dwPenColor));
    m_qPainter->drawEllipse(qRect);
}

void QtPaint::fillGradientEllipse(Long dwFirst, Long dwSecond, const FCRect& rect, int angle){
    fillEllipse(dwFirst, rect);
}

void QtPaint::fillGradientPath(Long dwFirst, Long dwSecond, const FCRect& rect, int angle){
    fillPath(dwFirst);
}

void QtPaint::fillGradientPolygon(Long dwFirst, Long dwSecond, FCPoint *apt, int cpt, int angle){
    fillPolygon(dwFirst, apt, cpt);
}

void QtPaint::fillGradientRect(Long dwFirst, Long dwSecond, const FCRect& rect, int cornerRadius, int angle){
    fillRect(dwFirst, rect);
}

void QtPaint::fillPath(Long dwPenColor){
    if(dwPenColor == FCColor_None) return;
    m_qPainter->setPen(QColor(0, 0, 0, 0));
    m_qPainter->fillPath(*m_path, QBrush(getQColor(dwPenColor)));
}

void QtPaint::fillPie(Long dwPenColor, const FCRect& rect, float startAngle, float sweepAngle){
	if(dwPenColor == FCColor_None) return;
	int rw = rect.right - rect.left - 1;
    if (rw < 1) rw = 1;
    int rh = rect.bottom - rect.top - 1;
    if (rh < 1) rh = 1;
    QRect qRect(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
    affectScaleFactor(&qRect);
}

void QtPaint::fillPolygon(Long dwPenColor, FCPoint *apt, int cpt){
	if(dwPenColor == FCColor_None) return;
    QPoint *points = new QPoint[cpt];
    QPainterPath path;
	for(int i = 0; i < cpt; i++){
		int x = apt[i].x + m_offsetX;
		int y = apt[i].y + m_offsetY;
		if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
			x = (int)(m_scaleFactorX * x);
			y = (int)(m_scaleFactorY * y);
		}
        if (i == 0){
            path.moveTo(x, y);
        }else{
            path.lineTo(x, y);
        }
    }
    path.closeSubpath();
    m_qPainter->fillPath(path, QBrush(getQColor(dwPenColor)));
    m_qPainter->setPen(QColor(0, 0, 0, 0));
	delete[] points;
	points = 0;
}

void QtPaint::fillRect(Long dwPenColor, const FCRect& rect){
	fillRect(dwPenColor, rect.left, rect.top, rect.right, rect.bottom);
}

void QtPaint::fillRect(Long dwPenColor, int left, int top, int right, int bottom){
	if(dwPenColor == FCColor_None) return;
    QRect qRect(left + m_offsetX, top + m_offsetY, right - left, bottom - top);
    affectScaleFactor(&qRect);
    m_qPainter->setPen(QColor(0, 0, 0, 0));
    m_qPainter->fillRect(qRect, QBrush(getQColor(dwPenColor)));
}

void QtPaint::fillRoundRect(Long dwPenColor, const FCRect& rect, int cornerRadius){
    if(cornerRadius > 0){
        if(dwPenColor == FCColor_None) return;
        QRect qRect(rect.left + m_offsetX, rect.top + m_offsetY, rect.right - rect.left, rect.bottom - rect.top);
        affectScaleFactor(&qRect);
        m_qPainter->setPen(QColor(0, 0, 0, 0));
        m_qPainter->setBrush(getQColor(dwPenColor));
        int corner = (int)(min(m_scaleFactorX, m_scaleFactorY) * cornerRadius);
        m_qPainter->drawRoundedRect(qRect, corner, corner);
    }else{
        fillRect(dwPenColor, rect);
    }
}

Long QtPaint::getColor(Long dwPenColor){
	return m_myColor->getUserColor(dwPenColor);
}

Long QtPaint::getPaintColor(Long dwPenColor){
	return getColor(dwPenColor);
}

FCPoint QtPaint::getOffset(){
	FCPoint offset = {m_offsetX, m_offsetY};
	return offset;
}

void QtPaint::getScaleFactor(double *scaleFactorX, double *scaleFactorY){
	*scaleFactorX = m_scaleFactorX;
	*scaleFactorY = m_scaleFactorY;
}

FCPoint QtPaint::rotate(const FCPoint& op, const FCPoint& mp, int angle){
    float PI = 3.14159265f;
	FCPoint pt = {0};
    pt.x = (int)((mp.x - op.x) * cos(angle * PI / 180) - (mp.y - op.y) * sin(angle * PI / 180) + op.x);
    pt.y = (int)((mp.x - op.x) * sin(angle * PI / 180) + (mp.y - op.y) * cos(angle * PI / 180) + op.y);
    return pt;
}

void QtPaint::setClip(const FCRect& rect){
    QRect qRect(rect.left + m_offsetX, rect.top + m_offsetY, rect.right - rect.left, rect.bottom - rect.top);
    affectScaleFactor(&qRect);
    m_qPainter->setClipRect(qRect);
}

void QtPaint::setLineCap(int startLineCap, int endLineCap){
}

void QtPaint::setOffset(const FCPoint& offset){
	m_offsetX = offset.x;
	m_offsetY = offset.y;
}

void QtPaint::setOpacity(float opacity){
	if(m_opacity != opacity){
        m_opacity = opacity;
	}
}

void QtPaint::setResourcePath(const String& resourcePath){
	m_resourcePath = resourcePath;
}

void QtPaint::setRotateAngle(int rotateAngle){
	m_rotateAngle = rotateAngle;
}

void QtPaint::setScaleFactor(double scaleFactorX, double scaleFactorY){
	m_scaleFactorX = scaleFactorX;
	m_scaleFactorY = scaleFactorY;
}

bool QtPaint::supportTransparent(){
	return true;
}

FCSize QtPaint::textSize(String strText, FCFont *font, int width){
    if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
        float rate = (m_scaleFactorX + m_scaleFactorY) / 2;
        float fontSize = (float)(font->m_fontSize * rate);
        FCFont scaleFont(font->m_fontFamily, fontSize, font->m_bold, font->m_underline, font->m_italic);
        QFont qFont = getFont(&scaleFont);
        QFontMetrics metrics(qFont);
        FCSize size = {(int)(metrics.horizontalAdvance(toQString(strText)) / rate), (int)(metrics.ascent() / rate)};
        return size;
    }else{
        QFont qFont = getFont(font);
        QFontMetrics metrics(qFont);
        FCSize size = {(int)metrics.horizontalAdvance(toQString(strText)), (int)metrics.ascent()};
        return size;
    }
}

FCSizeF QtPaint::textSizeF(String strText, FCFont *font, int width){
    FCSize size = textSize(strText, font, width);
    FCSizeF fSize = {(float)size.cx, (float)size.cy};
    return fSize;
}
